package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2023/04/05 15:27
 */
public class UserServiceImpl implements UserService {
    @Override
    public CommonData query(String mobile, Integer userId,Integer page, Integer limit, String sort, String order, String username) {
        TransactionUtil.startTransaction();
        MarketUserMapper marketUserMapper = TransactionUtil.getMapper(MarketUserMapper.class);
        // 1开启分页
        PageHelper.startPage(page, limit);

        // 2执行查询
        // a.构造Example对象，这个对象就是条件的容器
        MarketUserExample example = new MarketUserExample();
        // b.向这个容器里添加条件
        //    username like ?
        //    password = ?
        //    age = ?
        //    stats in (?,?,?)
        // 构造了一个空的条件的列表 → 目的是向这个列表里添加条件
        MarketUserExample.Criteria criteria = example.createCriteria();//List<条件>

        if (!StringUtil.isEmpty(username)){

            criteria.andUsernameLike("%" + username + "%");

        }
        if (userId!=null){

            criteria.andIdEqualTo(userId);

        }

        if(!StringUtil.isEmpty(mobile)){

            criteria.andMobileEqualTo(mobile);

        }

                /*.andPasswordEqualTo("123456")
                .andGenderEqualTo((byte) 1);*/

        example.setOrderByClause(sort + " " + order);
        List<MarketUser> users = marketUserMapper.selectByExample(example);

        // 3获得分页信息并封装
        PageInfo<MarketUser> pageInfo = new PageInfo<>(users);
        /*CommonData data = new CommonData();
        data.setTotal((int) pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setPage(pageInfo.getPageNum());
        data.setLimit(pageInfo.getPageSize());
        data.setList(pageInfo.getList());
        return data;*/
        CommonData data = CommonData.data(pageInfo);
        TransactionUtil.commit();
        return data;
    }

    @Override
    public void updateUser(Integer id, String password, String mobile, Integer gender, String nickname,Integer status, Integer userLevel) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession= TransactionUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByPrimaryKey(id);

        Date date = new Date();

        marketUser.setUpdateTime(date);
        marketUser.setGender( (byte) gender.intValue());


        marketUser.setMobile(mobile);
        marketUser.setNickname(nickname);
        marketUser.setStatus( (byte) status.intValue());
        marketUser.setUserLevel( (byte) userLevel.intValue());
        marketUser.setPassword(password);

        int i = mapper.updateByPrimaryKeySelective(marketUser);
        if (sqlSession!=null){

            sqlSession.commit();

            sqlSession.close();
        }


    }

    @Override
    public MarketUser queryById(Integer id, Integer page, Integer limit, Integer sort, Integer order) {

        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();

        MarketUserExample example = new MarketUserExample();

        MarketUserExample.Criteria criteria = example.createCriteria();

        if (id!=null){

            criteria.andIdEqualTo(id);

        }


        // example.setOrderByClause(sort + " " + order);
        //PageHelper.startPage(page, limit);


        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);


        MarketUser marketUser = mapper.selectByPrimaryKey(id);



        if (sqlSession!=null){

            sqlSession.close();
        }



        return marketUser;




    }

}
